import { defineStore } from 'pinia'
import type { IGoods } from '@/biz/repo/good'
import { computed, reactive } from 'vue'
/* 
购物车pinia状态管理
state 数据
添加购物车数据
移除购物车数据
清空购物车数据

获取商品数量
总数量
总价格-新
总价格-旧
全选
设置选择框
*/
interface ICartState {
  items: IGoods[]
  checkedIds: number[] //选中的商品ID数组
}
//1、定义definStore
export const useCartStore = defineStore('cart', () => {
  const state: ICartState = reactive({
    items: [],
    checkedIds: []
  })
  //购物车里面的商品总数量(包含已选或未选)
  const total = computed(() => {
    return state.items.reduce((p, v) => p + v.cartCount, 0)
  })

  const totalSelect = computed(() => {
    const list = state.items.filter((v) => v.checked)
    return list.reduce((p, v) => p + v.cartCount, 0)
  })

  //计算被选中的商品的总价格
  const totalPrice = computed(() => {
    const list = state.items.filter((v) => v.checked)
    return list.reduce((p, v) => p + v.cartCount * v.price, 0).toFixed(2)
  })

  //计算被选中的商品的总价格-没有折扣
  const totalOldPrice = computed(() => {
    const list = state.items.filter((v) => v.checked)
    return list.reduce((p, v) => p + v.cartCount * v.oldPrice, 0).toFixed(2)
  })

  //检查是否是全选
  const isAllChecked = computed(() => {
    return state.items.length === state.checkedIds.length
  })

  //通过商品id获取选中的商品数量
  const cartCountById = (id: number) => {
    return state.items.find((v) => v.id == id)?.cartCount
  }

  //把商品添加到购物车中
  const pushProductToCart = (item: IGoods) => {
    //先判断items中是否有这类商品，有则数量加1，否则把商品添加到items中
    const cartItem = state.items.find((v) => v.id == item.id)
    if (cartItem) {
      cartItem.cartCount++
      return
    }
    item.cartCount = 1
    item.checked = true
    state.items.push(item)
    state.checkedIds.push(item.id)
  }

  //把商品减1或者从购物车中移除
  const removeProductToCart = (item: IGoods) => {
    const cartItemIndex = state.items.findIndex((v) => v.id == item.id)
    const cartItem = state.items[cartItemIndex]
    if (cartItem) {
      cartItem.cartCount--
      if (cartItem.cartCount === 0) {
        state.items.splice(cartItemIndex, 1)
        const index = state.checkedIds.findIndex((v) => v == item.id)
        if (index > -1) {
          state.checkedIds.splice(index, 1)
        }
      }
    }
  }

  //点击单个复选框把当前的ids从数组中添加或移除
  const pushOrRemoveCheckIds = (ids: number[]) => {
    state.checkedIds = ids
    const itemIds = state.items.map((v) => v.id)
    const fids = itemIds.filter((v) => !ids.some((e) => e == v))
    fids.forEach((v) => {
      const d = state.items.find((item) => item.id == v)
      if (d) {
        d.checked = false
      }
    })
    ids.forEach((v) => {
      const d = state.items.find((item) => item.id == v)
      if (d) {
        d.checked = true
      }
    })
  }

  //全选
  const toggleAllChecked = (isAllChecked: boolean) => {
    const ids = isAllChecked ? state.items.map((v) => v.id) : []
    state.checkedIds = ids
    //items里面的checked需要重置一下
    if (isAllChecked) {
      state.items.forEach((v) => (v.checked = true))
    } else {
      state.items.forEach((v) => (v.checked = false))
    }
  }

  //清空购物车
  const clearCart = () => {
    state.items = []
    state.checkedIds = []
  }

  return {
    state,
    total,
    totalSelect,
    totalPrice,
    totalOldPrice,
    isAllChecked,
    cartCountById,
    pushProductToCart,
    removeProductToCart,
    toggleAllChecked,
    clearCart,
    pushOrRemoveCheckIds
  }
})
